package com.yuntian.baidu;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuntian.baidu.conf.CusConfig;
import com.yuntian.entity.DimOaOrgMemberF;
import com.yuntian.mapper.DimOaOrgMemberFMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class UserService  extends ServiceImpl<DimOaOrgMemberFMapper, DimOaOrgMemberF> {

    private final CusConfig cusConfig;

    private final DeptService deptService;

    private final YthService ythService;


    public void queryUser(){
        List<YthService.EmpData> allEmpData = ythService.getAllEmpData();
        List<DimOaOrgMemberF> list = allEmpData.stream().map(v->{
            var u = new DimOaOrgMemberF();
            u.setId(v.getId());
            u.setName(v.getName());
            u.setCode(v.getCode());
            u.setMobile(v.getMobile());
            u.setOrgId(v.getOrg_id());
            u.setOrgName(v.getOrg_name());
            u.setOrgParentId(v.getOrg_parent_id());
            u.setPostId(v.getPost_id());
            u.setPostName(v.getPost_name());
            u.setOrgPathList(v.getOrg_path_list());
            u.setOrgType(v.getOrg_type());
            return u;
        }).collect(Collectors.toList());
        ListUtil.split(list,500).forEach(v->{
            this.saveOrUpdateBatch(v);
        });

    }


    public void addUser(String name, String orgId) {
        JSONObject queryUserReq = new JSONObject();
        queryUserReq.set("keyword", name);
        queryUserReq.set("page", 1);
        queryUserReq.set("limit", 10);
        HttpResponse response = HttpUtil.createPost(cusConfig.getAb().getUrl() + cusConfig.getAb().getUserQueryUrl())
                .header(Header.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .header("Authorization", "Bearer " + cusConfig.getAb().getToken())
                .header("X-Authorization", "Bearer " + cusConfig.getAb().getToken())
                .body(queryUserReq.toString())
                .disableCache().execute();
        if (response.isOk()) {
            JSONObject oldUser = JSONUtil.parseObj(response.body());
            if (ObjUtil.isNotEmpty(oldUser)) {
                JSONObject result = oldUser.getJSONObject("result");
                if (ObjUtil.isNotEmpty(result)) {
                    JSONArray resultJSONArray = result.getJSONArray("result");
                    if (resultJSONArray.size() > 0) {
                        JSONObject userJsonObject = resultJSONArray
                                .stream().map(user -> (JSONObject) user)
                                .filter(user -> StrUtil.equals(user.getStr("name"), name))
                                .findFirst()
                                .get();
                        log.info("已经存在用户:{}", JSONUtil.toJsonStr(userJsonObject));
                    } else {
                        // TODO
                        /*String abOrgId = MapUtil.getStr(null, orgId, StrUtil.EMPTY);
                        JSONObject addUserReq = new JSONObject();
                        addUserReq.set("name", name);
                        addUserReq.set("description", "同步的用户");
                        addUserReq.set("mobile", "17688538142");
                        addUserReq.set("position", "员工");
                        addUserReq.set("role_name", "developer");
                        addUserReq.set("password", "Temp12138.");
                        addUserReq.set("department_id", abOrgId);
                        HttpResponse responseUser = HttpUtil.createPost(cusConfig.getAb().getUrl() + cusConfig.getAb().getUserAddUrl())
                                .header(Header.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                                .header(Header.AUTHORIZATION, "Bearer " + cusConfig.getAb().getToken())
                                .header("X-" + Header.AUTHORIZATION, "Bearer " + cusConfig.getAb().getToken())
                                .body(queryUserReq.toString())
                                .disableCache().execute();
                        if (responseUser.isOk()) {
                            JSONObject newUser = JSONUtil.parseObj(responseUser.body());
                            log.info("新增用户成功！:{}", JSONUtil.toJsonStr(newUser));
                        } else {
                            log.error("新增用户失败:{}", responseUser.body());

                        }*/
                    }

                }
            }
        } else {
            log.error("查询用户失败:{}", response.body());
        }

    }

}
